package org.rslai.tcedit.web.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.rslai.tcedit.domain.Page;
import org.rslai.tcedit.domain.Product;
import org.rslai.tcedit.util.HttpUtil;
import org.rslai.tcedit.util.SessionUtil;
import org.rslai.tcedit.web.core.AbstractMvcController;

/**
 * 产品列表
 * @author rslai
 *
 * @spring.bean name="/admin/productList.jspx"
 */
public class ProductListController extends AbstractMvcController {

	@Override
	public Map<String, Object> getModel(HttpServletRequest request, HttpServletResponse response) throws Exception {
		int pageIndex = HttpUtil.getInt(request, "page", 0);

		String action = HttpUtil.getString(request, "action", "").toLowerCase();
		if (action.equals("search") && pageIndex==0) {
			Map<String, String> parametersMap = new HashMap<String, String>();
			parametersMap.put("sName", HttpUtil.getString(request, "sName", ""));
			parametersMap.put("sDescribing", HttpUtil.getString(request, "sDescribing", ""));
			parametersMap.put("page", "1");
			SessionUtil.setParametersSessionInfo(request, parametersMap);
		}

		Map<String, String> parametersMap = SessionUtil.getParametersSessionInfo(request);
		String sName = parametersMap.get("sName");
		String sDescribing = parametersMap.get("sDescribing");
		if (pageIndex==0) {
			pageIndex = Integer.parseInt(parametersMap.get("page"));
		} else {
			parametersMap.put("page", String.valueOf(pageIndex));
		}

		Page page = new Page(pageIndex);
		List<Product> product = productService.query(page, "%"+sName+"%", "%"+sDescribing+"%", true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("product", product);
		model.put("page", page);
		model.put("sName", sName);
		model.put("sDescribing", sDescribing);
		return model;
	}

	@Override
	public String getView(HttpServletRequest request, HttpServletResponse response) {
		return "/admin/productList.htm";
	}

}
